﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CtCache;
using CtCommon.Utility;
using Cxx.BLL;
using Cxx.Common;
using Cxx.Entity;

namespace Cxx.OrderManagerSys
{
    public partial class StockRecordExport : System.Web.UI.Page
    {
        public string guid;

        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                guid = WebHelper.GetQueryString("Guid");
                if (Request.HttpMethod == "POST")
                {
                    if (string.IsNullOrEmpty(guid))
                    {
                        return;
                    }
                    string condition = CacheHelper.Instance.GetObject<string>(guid);
                    if (string.IsNullOrWhiteSpace(condition))
                    {
                        Response.Write("需要导出的内容已下载完成或没有需要导出的内容");
                        Response.End();
                        return;
                    }
                    List<StockRecordInfo> list = StockRecord.GetList(condition);
                    if (list == null || list.Count == 0)
                    {
                        JScript.Alert(Page, "没有可导出的内容");
                        return;
                    }
                    Dictionary<string, DataTable> dataTableDic;
                    SplitDataTable(list, out dataTableDic);

                    if (dataTableDic.Count != 0)
                    {
                        AsposeExcelHelper.GetInstance()
                            .ExportToWeb("澳门赌本操作记录-" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".xls", dataTableDic);
                        CacheHelper.Instance.RemoveObject(guid);
                    }
                }
            }
            catch (Exception exception)
            {
                CacheHelper.Instance.RemoveObject(guid);
                throw;
            }
        }

        /// <summary>
        /// 分解dataTable
        /// </summary>
        /// <param name="originalOrder"></param>
        /// <param name="dataTableDic"></param>
        /// <returns></returns>
        private static void SplitDataTable(List<StockRecordInfo> originalOrder, out Dictionary<string, DataTable> dataTableDic)
        {
            dataTableDic = new Dictionary<string, DataTable>();
            var exportList = TransferInfos(originalOrder);
            var table = TableHelper.GetDataTable(exportList, GetTitle());
            dataTableDic.Add("总表", table);
        }

        private static string[] GetTitle()
        {
            return new[] {"记录编号", "单号", "单位", "商品名称", "出入库类型", "数量", "创建时间", "入库号"};
        }

        private static List<ExportStockRecordInfo> TransferInfos(List<StockRecordInfo> list)
        {
            List<ExportStockRecordInfo> result = new List<ExportStockRecordInfo>();
            foreach (var stockRecordInfo in list)
            {
                ExportStockRecordInfo exportStockRecordInfo = new ExportStockRecordInfo
                {
                    CreateTime = stockRecordInfo.CreateTime.ToString("yyyy-MM-dd HH:mm:ss"),
                    OrderCode = stockRecordInfo.OrderCode,
                    Receiver = stockRecordInfo.Receiver,
                    StockType = ((StockTypes) stockRecordInfo.StockType).GetDescription(),
                    TotalCount = stockRecordInfo.TotalCount,
                    TradeName = stockRecordInfo.TradeName,
                    Id = stockRecordInfo.Id,
                    StockCode = stockRecordInfo.StockCode
                };
                result.Add(exportStockRecordInfo);
            }

            return result;
        }
    }
}